package online.flowerinsnow.minecraft.bukkit.testffa.manager;

import online.flowerinsnow.minecraft.bukkit.testffa.Main;
import online.flowerinsnow.minecraft.bukkit.testffa.util.ItemStackFactory;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class PlayerKitManager {
    private PlayerKitManager() {
    }

    @NotNull
    private static Map<Player, ConfigurationSection> playerKit = new HashMap<>();

    public static @NotNull ConfigurationSection loadPlayerKit(Player player) {
        File dir = new File(Main.getInstance().getDataFolder(), "kit");
        if (!dir.exists()) dir.mkdir();
        File file = new File(dir, player.getUniqueId() + ".yml");
        if (!file.exists()) {
            try {
                file.createNewFile();
                YamlConfiguration config = new YamlConfiguration();
                config.set("0", new ItemStackFactory(Material.DIAMOND_SWORD)
                        .addEnchantment(Enchantment.DAMAGE_ALL, 2, true)
                        .unbreakable()
                        .toItemStack());
                config.set("1", new ItemStack(Material.ENDER_PEARL, 16));
                config.set("2", new ItemStack(Material.COOKED_BEEF, 64));
                ItemStack pot = new ItemStack(Material.POTION, 1, (short) 16421);
                for (int i = 3; i < 36; i++) {
                    config.set(Integer.toString(i), pot);
                }
                config.save(file);
                playerKit.put(player, config);
                return config;
            } catch (IOException e) {
                player.kickPlayer("§c您的数据加载失败 若该问题持续痴线 请联系管理员");
                throw new RuntimeException(e);
            }
        }
        YamlConfiguration config = YamlConfiguration.loadConfiguration(file);
        playerKit.put(player, config);
        return config;
    }

    public static boolean unloadPlayerKit(@NotNull Player player) {
        return playerKit.remove(player) != null;
    }

    @Nullable
    public static ConfigurationSection getPlayerKit(@NotNull Player player) {
        return playerKit.get(player);
    }

    public static void savePlayerKit(@NotNull Player player, @NotNull FileConfiguration config) {
        playerKit.put(player, config);
        File dir = new File(Main.getInstance().getDataFolder(), "kit");
        if (!dir.exists()) dir.mkdir();
        File file = new File(dir, player.getUniqueId() + ".yml");
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            config.save(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
